import { defineStore } from 'pinia'
import { getProductList, getProductById, createProduct, updateProduct, deleteProduct, getProductVersions, getProductMembers } from '@/api/product'
import { message } from '@/utils/message'

// 定义产品状态管理
const useProductStore = defineStore('product', {
  state: () => ({
    // 产品列表
    productList: [],
    // 产品详情
    productDetail: null,
    // 产品版本列表
    productVersions: [],
    // 产品成员列表
    productMembers: [],
    // 分页信息
    pagination: {
      current: 1,
      size: 10,
      total: 0
    },
    // 加载状态
    loading: false,
    // 搜索条件
    searchParams: {}
  }),

  getters: {
    // 获取产品列表
    getProductList: (state) => state.productList,
    // 获取产品详情
    getProductDetail: (state) => state.productDetail,
    // 获取产品版本列表
    getProductVersions: (state) => state.productVersions,
    // 获取产品成员列表
    getProductMembers: (state) => state.productMembers,
    // 获取分页信息
    getPagination: (state) => state.pagination,
    // 获取加载状态
    getLoading: (state) => state.loading,
    // 获取搜索条件
    getSearchParams: (state) => state.searchParams
  },

  actions: {
    // 获取产品列表
    async getProductListAction(params = {}) {
      try {
        this.loading = true
        this.searchParams = params
        
        const response = await getProductList({
          page: params.page || this.pagination.current,
          pageSize: params.pageSize || this.pagination.size,
          name: params.name,
          status: params.status,
          createTimeStart: params.createTimeStart,
          createTimeEnd: params.createTimeEnd
        })
        
        this.productList = response.data.records || []
        this.pagination = {
          current: response.data.current || 1,
          size: response.data.size || 10,
          total: response.data.total || 0
        }
        
        return Promise.resolve(response)
      } catch (error) {
        message.error(error.message || '获取产品列表失败')
        return Promise.reject(error)
      } finally {
        this.loading = false
      }
    },

    // 获取产品详情
    async getProductDetailAction(productId) {
      try {
        this.loading = true
        const response = await getProductById(productId)
        this.productDetail = response.data
        return Promise.resolve(response)
      } catch (error) {
        message.error(error.message || '获取产品详情失败')
        return Promise.reject(error)
      } finally {
        this.loading = false
      }
    },

    // 创建产品
    async createProductAction(productData) {
      try {
        this.loading = true
        const response = await createProduct(productData)
        message.success('创建产品成功')
        return Promise.resolve(response)
      } catch (error) {
        message.error(error.message || '创建产品失败')
        return Promise.reject(error)
      } finally {
        this.loading = false
      }
    },

    // 更新产品
    async updateProductAction(productId, productData) {
      try {
        this.loading = true
        const response = await updateProduct(productId, productData)
        message.success('更新产品成功')
        return Promise.resolve(response)
      } catch (error) {
        message.error(error.message || '更新产品失败')
        return Promise.reject(error)
      } finally {
        this.loading = false
      }
    },

    // 删除产品
    async deleteProductAction(productId) {
      try {
        this.loading = true
        const response = await deleteProduct(productId)
        message.success('删除产品成功')
        return Promise.resolve(response)
      } catch (error) {
        message.error(error.message || '删除产品失败')
        return Promise.reject(error)
      } finally {
        this.loading = false
      }
    },

    // 获取产品版本列表
    async getProductVersionsAction(productId, params = {}) {
      try {
        this.loading = true
        const response = await getProductVersions(productId, params)
        this.productVersions = response.data || []
        return Promise.resolve(response)
      } catch (error) {
        message.error(error.message || '获取产品版本列表失败')
        return Promise.reject(error)
      } finally {
        this.loading = false
      }
    },

    // 获取产品成员列表
    async getProductMembersAction(productId, params = {}) {
      try {
        this.loading = true
        const response = await getProductMembers(productId, params)
        this.productMembers = response.data || []
        return Promise.resolve(response)
      } catch (error) {
        message.error(error.message || '获取产品成员列表失败')
        return Promise.reject(error)
      } finally {
        this.loading = false
      }
    },

    // 重置产品详情
    resetProductDetail() {
      this.productDetail = null
    },

    // 重置产品版本列表
    resetProductVersions() {
      this.productVersions = []
    },

    // 重置产品成员列表
    resetProductMembers() {
      this.productMembers = []
    },

    // 重置分页信息
    resetPagination() {
      this.pagination = {
        current: 1,
        size: 10,
        total: 0
      }
    }
  }
})

export default useProductStore